<template>
	<div class="app-org-sector">
		<a-dropdown @click="orgSelect">
			<div class="orgsector">
				<span>{{ selectedOrgName }}</span>
				<DownOutlined v-if="selectedOrgArray.length > 0" />
			</div>
			<template #overlay>
				<a-menu class="menu">
					<a-menu-item
						v-for="(item, index) in selectedOrgArray"
						:key="item.srforgsectorid"
					>
						{{ item.srforgsectorname }}
					</a-menu-item>
				</a-menu>
			</template>
		</a-dropdown>
	</div>
</template>

<script lang="ts" setup>
import { Http } from '@/http';
import { useAppStoreWithOut } from '@/store';
import { Ref } from 'vue';
import { DownOutlined } from '@ant-design/icons-vue';

import { OrgSectorProps } from './orgsector';
const props = defineProps(OrgSectorProps);

/**
 * 注入加载行为
 *
 * @memberof AppOrgSector
 */
const reload: Function | undefined = inject('reload');

/**
 * 选中组织部门id
 *
 * @type {string}
 * @memberof AppOrgSector
 */
const selectedOrgId: Ref<string> = ref('');

/**
 * 选中组织部门名称
 *
 * @type {string}
 * @memberof AppOrgSector
 */
const selectedOrgName: Ref<string> = ref('');

/**
 * 组织部门名称数组
 *
 * @type {Array<any>}
 * @memberof AppOrgSector
 */
const selectedOrgArray: Ref<Array<any>> = ref([]);

/**
 * 获取数据
 *
 * @memberof AppOrgSector
 */
const getData = () => {
	const appData = App.getAppData();
	if (appData) {
		let _context = appData.context;
		let _srforgname = appData.context.srforgname;
		let _srforgsectorname = appData.context.srforgsectorname;
		if (_context && appData.context.srforgsectorid) {
			selectedOrgId.value = appData.context.srforgsectorid;
		} else {
			return false;
		}

		if (_context && _srforgname && _srforgsectorname) {
			selectedOrgName.value = _srforgname + '-' + _srforgsectorname;
		} else if (_context && _srforgname) {
			selectedOrgName.value = _srforgname;
		} else if (_context && _srforgsectorname) {
			selectedOrgName.value = _srforgsectorname;
		}
		if (appData.srforgsections) {
			selectedOrgArray.value = appData.srforgsections;
		}
	}
};

onMounted(() => {
	getData();
});

/**
 * 调用远端切换部门接口
 *
 * @memberof AppOrgSector
 */
const switchDepartment = async (data: any) => {
	return await Http.getInstance().post(`/oumaps/switch`, data);
};

/**
 * 更新仓库部门信息
 *
 * @memberof AppOrgSector
 */
const updateStoreOrgData = (data: any) => {
	const appData = App.getAppData();
	let _context: any = appData.context;
	Object.assign(_context, data);
	Object.assign(appData, { context: _context });
	const { addAppData } = useAppStoreWithOut();
	addAppData(appData);
};

/**
 * 选择组织部门回调
 *
 * @memberof AppOrgSector
 */
const orgSelect = (data: string) => {
	if (Object.is(data, selectedOrgId.value)) {
		return;
	}
	let item: any = selectedOrgArray.value.find((_item: any) => {
		return _item.srforgsectorid === data;
	});
	switchDepartment(data)
		.then((response: any) => {
			if (response.status == 200) {
				if (item.srforgsectorid && item.srforgsectorname) {
					selectedOrgId.value = item.srforgsectorid;
					selectedOrgName.value = item.srforgsectorname;
				}
				updateStoreOrgData(item);
				reload?.();
			} else {
				App.getNotificationHelper().error(
					'错误',
					response.data ? response.data.message : '切换失败'
				);
			}
		})
		.catch((error: any) => {
			App.getNotificationHelper().error('错误', '切换失败');
		});
};
</script>
